package org.example;

import com.mysql.jdbc.Driver;

import java.sql.*;

public class JdbcLoginCode2 {
    static Connection con;
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
            con = DriverManager.getConnection("jdbc:mysql://121.41.77.32:3306/school_demo?useSSL=False","root","123456");

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static boolean login(String user,String pw) throws SQLException {
        String sql = "select * from user_config where users= ? and passwords=md5(?)";
        //sql预编译
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1,user);
        preparedStatement.setString(2,pw);
        ResultSet resultSet = preparedStatement.executeQuery();
        if(resultSet.next()){
            preparedStatement.close();
            return true;
        }else {
            preparedStatement.close();
            return false;
        }
    }

    public static void main(String[] args) throws SQLException {
        boolean res = login("shengzai","123456");
        if (res){
            System.out.println("登陆成功");
        }else {
            System.out.println("登陆失败");
        }
    }
}
